package com.hu.morser;

/**
 * @author Culex Consulting BV
 * All rights reserved.
 */

import java.io.IOException;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

public class Webservice {
	private static final String SOAP_ACTION_ENCODE = "http://tempury.org/encode";
	private static final String METHOD_NAME_ENCODE = "encode";
	
	private static final String SOAP_ACTION_DECODE = "http://tempury.org/decode";
	private static final String METHOD_NAME_DECODE = "decode";
	
	private static final String NAMESPACE = "http://tempury.org";
	private static final String URL = "http://145.89.153.185:8080/MorseServer/services/MorseFormatter?wsdl";
	
	public Webservice()
	{

	}
     
	public SoapObject Encode(String text, String seperator) 
	{ 
		SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME_ENCODE);
		Request.addProperty("text",text);
		Request.addProperty("seperator", seperator);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.setOutputSoapObject(Request);
		envelope.dotNet = true;
		
		HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
		androidHttpTransport.debug = true; 


		try
		{
			androidHttpTransport.call(SOAP_ACTION_ENCODE, envelope);
			SoapObject response = (SoapObject)envelope.bodyIn;
			return response;
		}
		catch(XmlPullParserException exPull)
		{
			exPull.printStackTrace();
			return null;
		}
		catch(IOException exIO)
		{
			exIO.printStackTrace();
			return null;
		}
	} 
	
	public SoapObject Decode(String text, String seperator) 
	{ 
		SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME_DECODE);
		Request.addProperty("text",text);
		Request.addProperty("seperator", seperator);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.setOutputSoapObject(Request);
		envelope.dotNet = true;
		
		HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
		androidHttpTransport.debug = true; 


		try
		{
			androidHttpTransport.call(SOAP_ACTION_DECODE, envelope);
			System.out.println("response: " + envelope.bodyIn);
			SoapObject response = (SoapObject)envelope.bodyIn;
			return response;
		}
		catch(XmlPullParserException exPull)
		{
			exPull.printStackTrace();
			return null;
		}
		catch(IOException exIO)
		{
			exIO.printStackTrace();
			return null;
		}
	} 

}
